package com.example.guiguiapplicatio.fragment.examination.adapter;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;

import com.example.guiguiapplicatio.R;
import com.example.guiguiapplicatio.databinding.FragmentExaminationPersonItemBinding;
import com.example.guiguiapplicatio.fragment.examination.placeholder.ExaminationPersonPlaceholderContent.PlaceholderItem;


import java.text.SimpleDateFormat;
import java.util.List;

public class ExaminationPersonAdapter extends RecyclerView.Adapter<ExaminationPersonAdapter.ViewHolder> {
    private final List<PlaceholderItem> mValues;
    private Context mContext;

    public ExaminationPersonAdapter(List<PlaceholderItem> mValues, Context mContext) {
        this.mValues = mValues;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(FragmentExaminationPersonItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PlaceholderItem currentItem = mValues.get(position);
        holder.binding.examinationName.setText(currentItem.getName());
        holder.binding.examinationSex.setText(currentItem.getSex());
        holder.binding.examinationAge.setText(String.valueOf(currentItem.getAge()));
        holder.binding.examinationPeopleType.setText(String.join("、", currentItem.getListenername()));
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formatDate = dateFormat.format(currentItem.getStarttime());
        holder.binding.examinationTime.setText(formatDate);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Integer examinationid = currentItem.getExaminationid();
                String name = currentItem.getName();
                Bundle bundle = new Bundle();
                bundle.putInt("examinationid", examinationid);
                bundle.putString("name", name);
                Navigation.findNavController(v).navigate(R.id.action_examinationPersonFragment_to_examinationTableFragment, bundle);
            }
        });
    }


    @Override
    public int getItemCount() {
        int itemCount = mValues.size();
        return itemCount;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        FragmentExaminationPersonItemBinding binding;

        public ViewHolder(@NonNull FragmentExaminationPersonItemBinding fragmentExaminationPersonItemBinding) {
            super(fragmentExaminationPersonItemBinding.getRoot());
            this.binding = fragmentExaminationPersonItemBinding;
        }
    }
}
